/* 生产管理-生产订单 */

import { ajaxService } from '@/utils/request';
import * as Type from './type';
import type { Tpage } from '../type';

const env = import.meta.env;
let baseURL = env.VITE_YUANTAO_SHENGCHAN_URL;

/**
 * 批量新增生产订单
 * @param data
 * @returns
 */
export function PostProduceOrders(data: {
  produceOrders: Type.ProduceOrder[];
}) {
  return ajaxService({
    url: '/produce/produceOrders',
    method: 'post',
    baseURL,
    data
  });
}
/**
 * 查询生产订单列表
 * @param data
 * @returns
 */
export function GetProduceOrders(params: Tpage & { keywoed: string }) {
  return ajaxService({
    url: '/produce/page',
    method: 'get',
    baseURL,
    params
  });
}
/**
 * 查询生产订单详情
 * @param data
 * @returns
 */
export function GetProduceOrderDetails(params: { id: any }) {
  return ajaxService<
    Type.ProduceOrder & { material?: Type.Tmaterial; bomId?: any }
  >({
    url: '/produce/produceOrder',
    method: 'get',
    baseURL,
    params
  });
}

/**
 * 删除生产订单
 * @param data
 * @returns
 */
export function DelProduceOrders(data: { id: number }) {
  return ajaxService({
    url: '/produce/produceOrder',
    method: 'DELETE',
    baseURL,
    data
  });
}
/**
 * 修改生产订单
 * @param data
 * @returns
 */
export function PutProduceOrders(
  data: Type.ProduceOrder & { material?: Type.Tmaterial }
) {
  return ajaxService({
    url: '/produce/produceOrder',
    method: 'put',
    baseURL,
    data
  });
}
/**
 * 修改生产订单
 * @param data.status 1:待确认 2:已确认 3:待排程 4:已排程 5:生产中 6:已完工
 * @returns
 */
export function PutProduceOrderStatus(data: {
  id: number;
  status: 1 | 2 | 3 | 4 | 5 | 6;
}) {
  return ajaxService({
    url: '/produce/produceOrder/status',
    method: 'put',
    baseURL,
    data
  });
}
/**
 * 查询mrp组件列表
 * @param data
 * @returns
 */
export function GetProduceOrdersMrp(params: { id: number }) {
  return ajaxService<Type.TElement[]>({
    url: '/produce/mrp/components',
    method: 'get',
    baseURL,
    params
  });
}

/**
 * 查询mrp物料列表
 * @param data
 * @returns
 */
export function GetProduceMaterialsMrp(params: { id: number }) {
  return ajaxService<Type.TDatum[]>({
    url: '/produce/mrp/materials',
    method: 'get',
    baseURL,
    params
  });
}

/**
 * 批量新增生产任务
 * @param data
 * @returns
 */
export function PostTasks(data: { produceTasks: Type.ProduceTask[] }) {
  return ajaxService({
    url: '/produce/tasks',
    method: 'post',
    baseURL,
    data
  });
}

/**
 * 分页生产任务列表
 * @param data
 * @returns
 */
export function GetTaskPage(
  params: Tpage & { code?: string; beginAt?: number; endAt?: number }
) {
  return ajaxService({
    url: '/produce/task/page',
    method: 'get',
    baseURL,
    params
  });
}

/**
 * 批量新增生产流程卡
 * @param data
 * @returns
 */
export function PostFlowCards(data: { flowCards: Array<Type.FlowCard> }) {
  return ajaxService({
    url: '/produce/flowCards',
    method: 'post',
    baseURL,
    data
  });
}

/**
 * 分页生产流程卡
 * @param data
 * @returns
 */
export function GetFlowCard(params: Tpage & { taskId: number }) {
  return ajaxService({
    url: '/produce/flowCard/page',
    method: 'get',
    baseURL,
    params
  });
}
/**
 * 打印单个
 * @param data
 * @returns
 */
export function GetFlowCardMain(params: { id: number }) {
  return ajaxService({
    url: '/produce/flowCard',
    method: 'get',
    baseURL,
    params
  });
}

/**
 * 打印多个
 * @param data
 * @returns
 */
export function GetFlowCardMains(params: { taskId: number }) {
  return ajaxService({
    url: '/produce/flowCards',
    method: 'get',
    baseURL,
    params
  });
}

/**
 * 生产派工新增
 * @param data
 * @returns
 */
export function PostDispatching(data: {
  dispatchings: {
    schedulingId: number;
    produceLineId: number;
    schedulingNumber: number;
    planBeginAt: number;
    planEndAt: number;
    planTime: number;
  }[];
}) {
  return ajaxService({
    url: '/produce/dispatchings',
    method: 'POST',
    baseURL,
    data
  });
}

/**
 * 查询生产派工
 * @param data
 * @returns
 */
export function GetDispatching(params: Tpage) {
  return ajaxService({
    url: '/produce/dispatching/page',
    method: 'GET',
    baseURL,
    params
  });
}
/**
 * 供派工项选择任务下拉列表
 * @param data
 * @returns
 */
export function GetTaskList(params: { id: number }) {
  return ajaxService({
    url: '/produce/dispatching/tasks',
    method: 'GET',
    baseURL,
    params
  });
}
/**
 * 修改生产派工
 * @param data
 * @returns
 */
export function PutDispatching(data: {
  id: number;
  taskId?: number;
  executorId?: number;
  isReport?: boolean;
}) {
  return ajaxService({
    url: '/produce/dispatching',
    method: 'PUT',
    baseURL,
    data
  });
}

/**
 * 获取工序列表根据任务id
 * @param data
 * @returns
 */
export function GetTaskProccessList(params: { taskId: number }) {
  return ajaxService({
    url: '/produce/proccessList',
    method: 'GET',
    baseURL,
    params
  });
}

/**
 * 派工列表查询根据任务和工序
 * @param data
 * @returns
 */
// taskId: number;
export function GetDispatchings(params: { processId: number }) {
  return ajaxService({
    url: '/produce/dispatchings',
    method: 'GET',
    baseURL,
    params
  });
}

/**
 * 获取报工列表
 * @param data
 * @returns
 */
export function GetReportWorks(params: { dispatchingId: number }) {
  return ajaxService({
    url: '/produce/reportWorks',
    method: 'GET',
    baseURL,
    params
  });
}

/**
 * 报工统计
 * @param data
 * @returns
 */
export function GetReportWorkStatistics(params: { dispatchingId: number }) {
  return ajaxService({
    url: '/produce/reportWorkStatistics',
    method: 'GET',
    baseURL,
    params
  });
}

/**
 * 查询sop
 * @param data
 * @returns
 */
export function GetSop(params: { processId: number }) {
  return ajaxService({
    url: '/factory/sop',
    method: 'GET',
    baseURL: env.VITE_FACTORY_URL,
    params
  });
}

/**
 * 新增报工
 * @param data
 * @returns
 */
export function PostReportWorks(data: {
  badProductNumber: number;
  /**
   * 派工id
   */
  dispatchingId: number;
  /**
   * 良品数
   */
  goodProductNumber: number;
  /**
   * 操作员id
   */
  operatorId: number;
  /**
   * 备注
   */
  remark: string;
  /**
   * 报工时长（分）
   */
  reportWorkTime: number;
}) {
  return ajaxService({
    url: '/produce/reportWork',
    method: 'POST',
    baseURL,
    data
  });
}

/**
 * 删除报工
 * @param data
 * @returns
 */
export function DelEReportWorks(data: {
  /**
   * 派工id
   */
  id: number;
}) {
  return ajaxService({
    url: '/produce/reportWork',
    method: 'DELETE',
    baseURL,
    data
  });
}
